﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class BaseData_AllLEVELCHANGECONDEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.LEVELCHANGECOND Property
    {
        get
        {
            Model.LEVELCHANGECOND property = null;
            if (ViewState["Property"] != null)
            {
                property = (Model.LEVELCHANGECOND)ViewState["Property"];
            }
            return property;
        }
        set
        {
            ViewState["Property"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400107");
        if (!IsPostBack)
        {            
            if (this.Code != "")
            {
                Label1.Text = "编辑页面";
                this.Property = new BLL.LEVELCHANGECOND().GetModel(int.Parse(this.Code));
                if (this.Property != null)
                {
                    this.Bind_carSet();  //会员等级 添加下拉框里的值
                    hID.Value = Property.LCC_ID.ToString();
                    txtType.SelectedValue = Property.LCC_TYPE;  //类别  积分，金额
                    DropDownList1.SelectedValue = Property.LCC_LEVEL;  //会员等级
                    LCC_BEGIN.Text  = Property.LCC_BEGIN.ToString()  ;   //累积起始值
                    LCC_END.Text = Property.LCC_END.ToString();       //累积结束值
                    LCC_ADDTIME.SelectedValue = Property.LCC_ADDTIME ;   //增量起始限
                    LCC_GEGINADD.Text = Property.LCC_GEGINADD.ToString();  //增量起始值
                    LCC_ENDADD.Text = Property.LCC_ENDADD.ToString();   //增量结束值 
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='AllLEVELCHANGECOND.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
                this.Bind_carSet();  //会员等级 添加下拉框里的值
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }

    protected void Bind_carSet() {
        List<Model.V_CARSET> properties = new BLL.V_CARSET().GetAllList();
        DropDownList1.DataSource = properties;
        DropDownList1.DataTextField = "MC";
        DropDownList1.DataValueField = "DM";
        DropDownList1.DataBind();
        ListItem li = new ListItem();
        li.Text = "请选择";
        li.Value = "-1";
        DropDownList1.Items.Insert(0, li);
    }

    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (Check.CheckPermission("300400107", "00001"))
            {
                if (LCC_BEGIN.Text.Length < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('开始值不能为空')", true);
                    return;
                }
                if (LCC_BEGIN.Text.Length < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('结束值不能为空')", true);
                    return;
                }
                if (DropDownList1.SelectedIndex < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择对应级别')", true);
                    return;
                }
                Model.LEVELCHANGECOND property = new Model.LEVELCHANGECOND();
                property.LCC_ID = Convert.ToInt32(txtType.SelectedValue);
                property.LCC_TYPE = txtType.SelectedValue;  //类别  积分，金额
                property.LCC_LEVEL = DropDownList1.SelectedValue  ;  //会员等级
                property.LCC_BEGIN = Convert.ToDecimal( LCC_BEGIN.Text=="" ? "0" : LCC_BEGIN.Text  );   //累积起始值
                property.LCC_END = Convert.ToDecimal(LCC_END.Text == "" ? "0" : LCC_END.Text  );       //累积结束值
                property.LCC_ADDTIME = LCC_ADDTIME.SelectedValue ;   //增量起始限
                property.LCC_GEGINADD = Convert.ToDecimal(LCC_GEGINADD.Text == "" ? "0" : LCC_GEGINADD.Text);  //增量起始值
                property.LCC_ENDADD = Convert.ToDecimal(LCC_ENDADD.Text == "" ? "0" : LCC_ENDADD.Text);   //增量结束值
              
                if (new BLL.LEVELCHANGECOND().Add(property) > 0)
                {
                    Share.InsertLog("300400107", "00001");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='AllLEVELCHANGECOND.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "添加记录失败,请重新添加。");
                }

            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='AllLEVELCHANGECOND.aspx';</script>");
            }
        }
        else
        {
            if (Check.CheckPermission("300400107", "00002"))
            {
                if (LCC_BEGIN.Text.Length < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('名称不能为空')", true);
                    return;
                }
                if (txtType.SelectedIndex < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择对应类型')", true);
                    return;
                }



                Property.LCC_ID = Convert.ToInt32(hID.Value );
                Property.LCC_TYPE = txtType.SelectedValue;  //类别  积分，金额
                Property.LCC_LEVEL = DropDownList1.SelectedValue;  //会员等级
                Property.LCC_BEGIN = Convert.ToDecimal(LCC_BEGIN.Text == "" ? "0" : LCC_BEGIN.Text);   //累积起始值
                Property.LCC_END = Convert.ToDecimal(LCC_END.Text == "" ? "0" : LCC_END.Text);       //累积结束值
                Property.LCC_ADDTIME = LCC_ADDTIME.SelectedValue;   //增量起始限
                Property.LCC_GEGINADD = Convert.ToDecimal(LCC_GEGINADD.Text == "" ? "0" : LCC_GEGINADD.Text);  //增量起始值
                Property.LCC_ENDADD = Convert.ToDecimal(LCC_ENDADD.Text == "" ? "0" : LCC_ENDADD.Text);   //增量结束值
              

                //Property.LCC_ID = Code;
                //Property.LCC_LEVEL = txtName.Text;
                //Property.LEVELCHANGECOND_TYPE = txtType.SelectedValue;
                //Property.LEVELCHANGECOND_CONTENT = m_myWebTextPane.Text;
                //Property.ISDEL = CKB_effect.Checked == true ? 0 : 1;
                //Property.REMARK = txtRemark.Text;
                if (new BLL.LEVELCHANGECOND().Update(this.Property))
                {
                    Share.InsertLog("300400107", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='AllLEVELCHANGECOND.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='AllLEVELCHANGECOND.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllLEVELCHANGECOND.aspx");
    }
    
}
